<?php

namespace falcon5\inc\Api;

use hollisho\helpers\ArrayHelper;
use hollisho\helpers\InvalidArgumentException;
use WP_Customize_Manager;


/**
 * Class CustomizeApi
 * @package falcon5\inc\Api
 * @desc 主题自定义API
 */
class CustomizeApi
{


    private $settings = [];

    private $sections = [];

    private $controls = [];

    private $panels = [];

    /**
     * @param array $settings
     * @return CustomizeApi
     */
    public function setSettings(array $settings): CustomizeApi
    {
        $this->settings = $settings;
        return $this;
    }

    /**
     * @param array $sections
     * @return CustomizeApi
     */
    public function setSections(array $sections): CustomizeApi
    {
        $this->sections = $sections;
        return $this;
    }

    /**
     * @param array $controls
     * @return CustomizeApi
     */
    public function setControls(array $controls): CustomizeApi
    {
        $this->controls = $controls;
        return $this;
    }

    /**
     * @param array $panels
     * @return CustomizeApi
     */
    public function setPanels(array $panels): CustomizeApi
    {
        $this->panels = $panels;
        return $this;
    }



    public function register()
    {
        add_action('customize_register', [$this, 'registerCustomizeSettings']);
    }

    /**
     * @param WP_Customize_Manager $wp_customize
     *
     * @throws InvalidArgumentException
     * @desc
     */
    public function registerCustomizeSettings(WP_Customize_Manager $wp_customize) {
        if ($this->settings) foreach ($this->settings as $setting) {
            $wp_customize->add_setting(ArrayHelper::getValue($setting, 'id'), [
                'type' => ArrayHelper::getValue($setting, 'type', 'theme_mod')
            ]);
        }

        if ($this->controls) foreach ($this->controls as $control) {
            $wp_customize->add_control(ArrayHelper::getValue($control, 'id'), [
                'section' => ArrayHelper::getValue($control, 'section'),
                'settings' => ArrayHelper::getValue($control, 'settings'),
                'label' => ArrayHelper::getValue($control, 'label'),
                'description' => ArrayHelper::getValue($control, 'description'),
            ]);
        }

        if ($this->sections) foreach ($this->sections as $section) {
            $wp_customize->add_section(ArrayHelper::getValue($section, 'id'), [
                'panel' => ArrayHelper::getValue($section, 'panel', ''),
                'title' => ArrayHelper::getValue($section, 'title'),
                'description' => ArrayHelper::getValue($section, 'description'),
            ]);
        }

        if ($this->panels) foreach ($this->panels as $panel) {
            $wp_customize->add_panel(ArrayHelper::getValue($panel, 'id'), [
                'title' => ArrayHelper::getValue($panel, 'title'),
                'description' => ArrayHelper::getValue($panel, 'description'),
            ]);
        }
    }
}